﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Game.Lib;

namespace Game
{
    public partial class GameServer : Form
    {
        ///设置数据库连接测试状态
        private bool passedDatabaseTest = false;
        //加载配置文件
        IniFiles iniFiles = new IniFiles(@".\setting.ini");
        //账号网关
        public AccountServer As = null;
        public GameServer()
        {
            InitializeComponent();
        }

        private void Server_Load(object sender, EventArgs e)
        {
            this.loadDatabaseSetting();
        }

        //加载数据库配置信息
        private void loadDatabaseSetting()
        {
            try
            {
                //获取配置信息
                string database_host = iniFiles.ReadString("database", "host", "localhost");
                int database_port = iniFiles.ReadInteger("database", "port", 3306);
                string database_user = iniFiles.ReadString("database", "user", "root");
                string database_pass = iniFiles.ReadString("database", "password", "");
                string database_name = iniFiles.ReadString("database", "name", "game");

                int account_gateway = iniFiles.ReadInteger("gateway", "account", 10001);
                int charater_gateway = iniFiles.ReadInteger("gateway", "character", 10002);
                int game_gateway = iniFiles.ReadInteger("gateway", "game", 10003);

                //加载到文本框
                textBoxDatabaseHost.Text = database_host;
                textBoxDatabaseName.Text = database_name;
                textBoxDatabasePort.Text = database_port.ToString();
                textBoxDatabaseUserName.Text = database_user;
                textBoxDatabasePassword.Text = database_pass;

                textBoxAccountGateway.Text = account_gateway.ToString();
                textBoxCharacterGateway.Text = charater_gateway.ToString();
                textBoxGameGateway.Text = game_gateway.ToString();
            }
            catch (ApplicationException err)
            {
                Log.Write("[ApplicationException]:" + err.Message);
            }
        }

        private void buttonTestDatabase_Click(object sender, EventArgs e)
        {
            buttonTestDatabase.Enabled = false;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                Database database = new Database(textBoxDatabaseHost.Text, System.Int32.Parse(textBoxDatabasePort.Text), textBoxDatabaseUserName.Text, textBoxDatabasePassword.Text, textBoxDatabaseName.Text);
                passedDatabaseTest = true;
                MessageBox.Show("数据库连接成功!", "系统提示");
            }
            catch (ApplicationException err)
            {
                passedDatabaseTest = false;
                MessageBox.Show(err.Message, "系统提示");
            }
            finally
            {
                buttonTestDatabase.Enabled = true;
                Cursor.Current = Cursors.Arrow;
            }
        }

        private void buttonStartAccountServer_Click(object sender, EventArgs e)
        {
            if (As == null)
            {
                As = new AccountServer();
                As.Owner = this;
                As.Show();
            }
            buttonStartAccountServer.Enabled = false;
            buttonStopAccountServer.Enabled = true;
            listBoxLogs.Items.Add(String.Format("[{0}]账号网关启动.", DateTime.Now.ToShortTimeString()));
        }

        private void buttonStopAccountServer_Click(object sender, EventArgs e)
        {
            As.Close();
            As = null;
            buttonStopAccountServer.Enabled = false;
            buttonStartAccountServer.Enabled = true;
            listBoxLogs.Items.Add(String.Format("[{0}]账号网关停止.", DateTime.Now.ToShortTimeString()));
        }

        private void buttonSaveSetting_Click(object sender, EventArgs e)
        {
            if (passedDatabaseTest)
            {
                try
                {
                    string host = textBoxDatabaseHost.Text;
                    int port = System.Int32.Parse(textBoxDatabasePort.Text);
                    string uid = textBoxDatabaseUserName.Text;
                    string pwd = textBoxDatabasePassword.Text;
                    string name = textBoxDatabaseName.Text;

                    int account_gateway = System.Int32.Parse(textBoxAccountGateway.Text);
                    int character_gateway = System.Int32.Parse(textBoxCharacterGateway.Text);
                    int game_gateway = System.Int32.Parse(textBoxGameGateway.Text);

                    iniFiles.WriteString("database", "host", host);
                    iniFiles.WriteInteger("database", "port", port);
                    iniFiles.WriteString("database", "user", uid);
                    iniFiles.WriteString("database", "password", pwd);
                    iniFiles.WriteString("database", "name", name);

                    iniFiles.WriteInteger("gateway", "account", account_gateway);
                    iniFiles.WriteInteger("gateway", "character", character_gateway);
                    iniFiles.WriteInteger("gateway", "game", game_gateway);
                    MessageBox.Show("配置写入成功", "系统提示");
                }
                catch (ApplicationException err)
                {
                    MessageBox.Show(err.Message, "系统提示");
                }
            }
            else
            {
                MessageBox.Show("请先进行数据库连接测试", "系统提示");
            }
        }
    }
}
